package com.gexiaobao.pigeon.ui.fragment.mall.store

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.init
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.ext.initFooter
import com.gexiaobao.pigeon.app.ext.loadListData
import com.gexiaobao.pigeon.app.ext.loadServiceInit
import com.gexiaobao.pigeon.app.ext.showLoading
import com.gexiaobao.pigeon.app.util.Util
import com.gexiaobao.pigeon.databinding.FragmentMerchantStoreBinding
import com.gexiaobao.pigeon.ui.adapter.StoreHomeIndexAdapter
import com.gexiaobao.pigeon.viewmodel.MerchantStoresViewModel
import com.kingja.loadsir.core.LoadService
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat
import me.hgj.jetpackmvvm.util.LogUtils

/**
 * @Author      : hxw
 * @Date        : 2023/6/27 15:20
 * @Describe    : 商家店铺首页
 */
class FragmentMerchantStores : BaseFragment<MerchantStoresViewModel, FragmentMerchantStoreBinding>() {

    private val mAdapter: StoreHomeIndexAdapter by lazy { StoreHomeIndexAdapter(arrayListOf()) }
    private lateinit var loadsir: LoadService<Any>
    private var shopId = 0

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBarMenu.tvRedBarRightMenu.text = getString(R.string.shop_shop_information_title)
        mDatabind.includeBarMenu.tvRedBarRightMenu.isVisible = true
        shopId = requireArguments().getInt("shopId")
        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        mDatabind.layoutList.swipeRecyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State) {
                super.getItemOffsets(outRect, view, parent, state)
                outRect.right = Util.dp2px(context, 5f)//右间距5dp
                outRect.left = Util.dp2px(context, 5f);//左间距5dp
                outRect.top = Util.dp2px(context, 5f)//上间距5dp
                outRect.bottom = Util.dp2px(context, 5f)//下间距5dp
            }
        })
        mDatabind.layoutList.swipeRecyclerView.layoutManager = layoutManager
        mDatabind.layoutList.swipeRecyclerView.adapter = mAdapter
        mDatabind.layoutList.swipeRecyclerView.initFooter {
            //触发加载更多时请求数据
            mViewModel.getAppShopIndex(false, shopId)
        }

        //状态页配置
        loadsir = loadServiceInit(mDatabind.layoutList.swipeRefreshLayout) {
            //点击重试时触发的操作
            onRefresh()
        }

        mDatabind.layoutList.swipeRefreshLayout.init {
            onRefresh()
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            nav().navigateAction(R.id.action_to_fragment_store_goods_detail, Bundle().apply {
                putString("id", mAdapter.data[position].id.toString())
            })
        }
    }

    override fun onResume() {
        super.onResume()
        onRefresh()
    }

    private fun onRefresh() {
        loadsir.showLoading()
        mViewModel.getAppShopIndex(true, shopId)
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBarMenu.tvRedBarRightMenu, mDatabind.includeBarMenu.ivBack) {
            when (it) {
                mDatabind.includeBarMenu.ivBack -> {
                    nav().navigateUp()
                }

                mDatabind.includeBarMenu.tvRedBarRightMenu -> {
                    nav().navigateAction(R.id.action_to_fragment_Store_info, Bundle().apply {
                        putInt("shopId", shopId)
                    })
                }
            }
        }
    }

    override fun createObserver() {
        super.createObserver()
        /** 进店首页 */
        mViewModel.storeGoodListResult.observe(viewLifecycleOwner) {
            loadListData(it, mAdapter, loadsir, mDatabind.layoutList.swipeRecyclerView, mDatabind.layoutList.swipeRefreshLayout)
            mDatabind.tvShopName.text = it.shopName
            mDatabind.includeBarMenu.tvWhiteTitle.text = it.shopName
        }
    }
}